<?php
class Product extends Vnf_Db_Table{
    protected $_name;
    protected $_primary = 'ProductNo';

    function __construct() {
        $this->_name = DB_PREFIX . "product";
        parent::__construct();
    }

    public function getPrimaryKey() {
        $pk = $this->_primary;
        if (is_array($pk)) {
            if (count($pk) > 0)
                return $pk[1]; // array[1] contain column name of primary key
        }
        return $this->_primary;
    }
    
    public function getAllCat() {
        $table_name = $this->_name;
        $primary_key = $this->getPrimaryKey();

        $select = Zend_Db_Table_Abstract::select();
        $select->setIntegrityCheck(false);
        
        $select->from(array('thistable' => $this->_name), array('Category'))
                ->distinct('Category')
                ->where('Category !=""')
                ->order('Category ASC')
        ;
        
        $data = $this->fetchAll($select);
        return $data;
    }
    
    public function getRecords($where = null, $order = null, $count = null, $offset = 0) {
        $table_name = $this->_name;
        $primary_key = $this->getPrimaryKey();
        $select = $select = Zend_Db_Table_Abstract::select();
        $select->setIntegrityCheck(false)
            ->from(array('thistable' => $table_name))
            ->joinLeft( array( 'f0table' => DB_PREFIX . "attribute"), "thistable.ProductNo = f0table.ProductNo", array('AttPrice' => 'Price', 'AttCost' => 'Cost', 'SKU'=>'SKU' ))
            ->group('ProductNo')
            ;
        if($where) {
            $select->where($where);
        }
        if($order) {
            $select->order($order);
        }
        if($count)
            $select->limit($count, $offset);
            
        //echo $select;
        $data = $this->fetchAll($select);
        return $data;
    }
    
    /*
    public function getTotalRecords($where = null, $cond = null) {
        $select = Zend_Db_Table_Abstract::select();
        $select->setIntegrityCheck(false)
            ->from(array('thistable' => $this->_name), array('cnt' => "count('*')"))
            ->joinLeft( array( 'f0table' => DB_PREFIX . "attribute"), "thistable.ProductNo = f0table.ProductNo")
            //->group('thistable.ProductNo')
        ;
        if ($where) {
            if (is_array($cond))
                $select->where($where, $cond);
            else $select->where($where);
        }

        echo $select;die;
        $result = 0;
        $data = $this->fetchRow($select);
        @$result = $data->cnt;

        return $result;
    }
    */
}
?>
